package com.sun.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.Charset;
import java.util.List;

/**
 * @author SunRui
 * @description 消息转换器
 * @date 2024/9/30 10:43
 */
@EnableWebMvc
@ComponentScan({"com.sun"})
public class MvcConfiguration implements WebMvcConfigurer {
    public MvcConfiguration() {
        System.out.println("mvc configuration constructor...");
    }

    //    通过@EnableWebMVC配置的时候起作用，
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {

        for (HttpMessageConverter httpMessageConverter : converters) {
            if (StringHttpMessageConverter.class.isAssignableFrom(httpMessageConverter.getClass())) {
                ((StringHttpMessageConverter) httpMessageConverter).setDefaultCharset(Charset.forName("UTF-8"));
            }
        }
    }

    // 文件上传解析器
    @Bean
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();

        commonsMultipartResolver.setMaxUploadSize(1024 * 10244 * 2);
        commonsMultipartResolver.setDefaultEncoding("UTF-8");

        return commonsMultipartResolver;
    }

}